package com.benefm.multipar.pad.util.filter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 【注意事项】
 * 1、每个设备需要单独创建一个EcgFilter对象进行滤波，不能多个设备数据使用同一个EcgFilter对象！！！！！！！！！！！！！
 */
public class Test {
    EcgFilter mEcgFilter = new EcgFilter();

    public void testFilter() {
        int i = 0;
        int j = 0;
        // 模拟生成心电数据
        Map<Integer, ArrayList<Short>> mapEcgData = new HashMap<Integer, ArrayList<Short>>();
        byte[] xLeadDictionary = new byte[8];
        //0 1
        for (i = 0; i < 2; i++) {
            xLeadDictionary[i] = (byte) i;
        }
        //6 7 8 9 10 11
        for (j = EcgDataContext.LEAD_V1; j <= EcgDataContext.LEAD_V6; j++) {
            xLeadDictionary[i++] = (byte) j;
        }
        for (i = 0; i < xLeadDictionary.length; i++) {
            ArrayList<Short> listEcgData = new ArrayList<Short>();
            for (j = 0; j < 250; j++) {
                listEcgData.add((short) j);
            }
            mapEcgData.put((int) xLeadDictionary[i], listEcgData);
        }
        // 调用滤波
        mapEcgData = filter(mapEcgData, xLeadDictionary);
    }

    /**
     * 滤波前数据处理：
     * 1、将数据转为ABAB格式
     * 2、扩展起搏导联数据
     *
     * @param mapEcgData      待滤波的数据
     * @param xLeadDictionary 导联字典
     * @return 滤波后的数据
     */
    public Map<Integer, ArrayList<Short>> filter(Map<Integer, ArrayList<Short>> mapEcgData, byte[] xLeadDictionary) {
        int i, j;
        int index = 0;
        int leadIndex = 0;
        int nOneLeadDataLength = 0;
        short[] xFilterData = new short[0];
        boolean bPacer = false;
        byte[] xLeadDic;
        Map<Integer, ArrayList<Short>> mapFilterData = new HashMap<Integer, ArrayList<Short>>();
        do {
            Iterator<Map.Entry<Integer, ArrayList<Short>>> it = mapEcgData.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Integer, ArrayList<Short>> entry = it.next();
                if (0 >= nOneLeadDataLength) {
                    nOneLeadDataLength = entry.getValue().size();
                }
            }
            int leadNum = mapEcgData.size();
            if (!mapEcgData.containsKey(EcgDataContext.LEAD_Pacer)) {
                leadNum++;
            }
            xLeadDic = new byte[leadNum];
            System.arraycopy(xLeadDictionary, 0, xLeadDic, 0, xLeadDictionary.length);
            for (i = 0, bPacer = false; i < xLeadDic.length; i++) {
                if (EcgDataContext.LEAD_Pacer == xLeadDic[i]) {
                    bPacer = true;
                    break;
                }
            }
            if (!bPacer) {
                xLeadDic[xLeadDic.length - 1] = EcgDataContext.LEAD_Pacer;
            }
            short[] xSrcDataBuf = new short[nOneLeadDataLength * leadNum];
            List<Map.Entry<Integer, ArrayList<Short>>> listEcgData = new ArrayList<Map.Entry<Integer, ArrayList<Short>>>(mapEcgData.entrySet());
            // 遍历数据，转为byte数组
            ArrayList<Short> xEcg;
            for (i = 0, j = 0; i < nOneLeadDataLength; i++) {
                for (Map.Entry<Integer, ArrayList<Short>> item : listEcgData) {
                    xEcg = item.getValue();
                    xSrcDataBuf[j++] = xEcg.get(i);
                }
                if (!bPacer) {
                    xSrcDataBuf[j++] = 0;
                }
            }

            // 滤波
            xFilterData = mEcgFilter.doFilter(xSrcDataBuf, xLeadDic, true, true);

            // 组装滤波后的数据
            for (i = 0; i < xLeadDictionary.length; i++) {
                ArrayList<Short> listData = new ArrayList<Short>();
                leadIndex = xLeadDictionary[i];
                index = i * nOneLeadDataLength;
                for (j = 0; j < nOneLeadDataLength; j++) {
                    listData.add(xFilterData[index + j]);
                }
                mapFilterData.put(leadIndex, listData);
            }
        } while (false);
        return mapFilterData;
    }
}
